package dev.vbms.autopilot.gui;

import com.jme3.app.Application;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
import java.awt.Dimension;
import java.awt.FlowLayout;

public class JMonkeyPanel extends javax.swing.JPanel {
    
    Application main;
    
    public void resizeRenderArea (Dimension dimension) {
        AppSettings settings = new AppSettings(true);
        settings.setWidth((int)dimension.getWidth());
        settings.setHeight((int)dimension.getHeight());
        main.setSettings(settings);
        JmeCanvasContext ctx = (JmeCanvasContext) main.getContext();
        ctx.getCanvas().setPreferredSize(dimension);
    }
    
    public void run (Application main) {
        // create new JME appsettings
        this.main = main;
        AppSettings settings = new AppSettings(true);
        Dimension dimension = this.getParent().getSize();
        int width = (int) dimension.getWidth();
        int height = (int) dimension.getHeight();
        settings.setWidth(width);
        settings.setHeight(height);
        // create new canvas application
        main.setSettings(settings);
        main.createCanvas();
        JmeCanvasContext ctx = (JmeCanvasContext) main.getContext();
        ctx.setSystemListener(main);
        ctx.getCanvas().setPreferredSize(dimension);
        setLayout(new FlowLayout());
        add(ctx.getCanvas());
        // start the application
        main.startCanvas();
    }
    
    public JMonkeyPanel() {
        // initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        resizeRenderArea(getSize());
    }//GEN-LAST:event_formComponentResized

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
